package com.software.frontend.loginAndRegist;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.software.frontend.R;
import com.software.frontend.loginAndRegist.utils.Result;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LoginAndRegisterByPhoneForVerificationCodeActivity extends AppCompatActivity {

    TextView textSelectPhoneLocation, toLoginByPhoneAndPasswordPage;
    Spinner spinnerSelectPhoneLocation;
    EditText phone;
    Button getVarificationCodeButton;
    String[] phoneLocations;
    String loginPhoneNumber;
    OkHttpClient okHttpClient = new OkHttpClient();
    Result result;
    private static final String serviceLocation = "login/loginOrRegisterByPhoneNumberForVarification";
    private static final Gson gson = new Gson();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_and_register_by_phone_for_verification_code);

        staticDataInit();

        initViews();

        setListneners();

    }

    private void staticDataInit() {
        phoneLocations = getResources().getStringArray(R.array.Phone_location_number);
    }


    private void initViews() {
        textSelectPhoneLocation = (TextView) findViewById(R.id.text_select_phone_location);
        spinnerSelectPhoneLocation = (Spinner) findViewById(R.id.spinner_select_phone_location);
        phone = (EditText) findViewById(R.id.editTextPhone);
        getVarificationCodeButton = (Button) findViewById(R.id.login_by_phone_and_varification_code_sure);
        toLoginByPhoneAndPasswordPage = (TextView) findViewById(R.id.text_to_login_by_password);
    }

    private void setListneners() {

        //电话属地选择监听
        spinnerSelectPhoneLocation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (phoneLocations[position].equals("...")){
                    Toast.makeText(LoginAndRegisterByPhoneForVerificationCodeActivity.this, "更多地区正在交洽...", Toast.LENGTH_SHORT).show();
                }else{
                    textSelectPhoneLocation.setText(phoneLocations[position]);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //登陆按钮监听
        getVarificationCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                //测试
//                Intent toVarifyCode = new Intent(LoginAndRegisterByPhoneForVerificationCodeActivity.this, LoginAndRegisiterByPhoneVarifiyCodeActivity.class);
//                toVarifyCode.putExtra("msg", "成功");
//                toVarifyCode.putExtra("phoneNumber", "13931536020");
//                startActivity(toVarifyCode);
//
                loginPhoneNumber = "18830186550";
                phone.setText(loginPhoneNumber);


                if (phone.getText().toString().length()!=11){
                    Toast.makeText(LoginAndRegisterByPhoneForVerificationCodeActivity.this, "请输入11位电话号", Toast.LENGTH_SHORT).show();
                }else {
                    FormBody formBody = new FormBody.Builder()
                            .add("phoneNumber", phone.getText().toString())
                            .build();
                    Request request = new Request.Builder().url(getResources().getString(R.string.localhost) + serviceLocation).post(formBody).build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            e.printStackTrace();
                            Log.i("onFailure" , "访问失败");
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String s = response.body().string();
                            result = gson.fromJson(s, Result.class);
                            if (result != null && result.getCode() == 200){
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        loginPhoneNumber = phone.getText().toString();
                                    }
                                });
                                Intent toVarifyCode = new Intent(LoginAndRegisterByPhoneForVerificationCodeActivity.this, LoginAndRegisiterByPhoneVarifiyCodeActivity.class);
                                toVarifyCode.putExtra("msg", result.getMsg());
                                toVarifyCode.putExtra("phoneNumber", phone.getText().toString());
                                startActivity(toVarifyCode);
                            } else {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(LoginAndRegisterByPhoneForVerificationCodeActivity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    });
                }

            }
        });


        //跳转密码登陆按钮监听
        toLoginByPhoneAndPasswordPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent loginToMain = new Intent(LoginAndRegisterByPhoneForVerificationCodeActivity.this, LoginByPhoneAndPasswordActivity.class);
                startActivity(loginToMain);
            }
        });
    }
}